/*
 * endian.c
 *
 * Created: 8/21/2013 3:13:50 PM
 *  Author: Ken Arok
 *
 * \file
 *
 * \brief Endianness Utilities.
 *
 * Copyright (c) 2013 PT Hanindo Automation Solutions. All rights reserved.
 *
 */


#include "endian.h"

void BigEndian_StoreByte(void *addr, unsigned char data)
{
	unsigned char *_p = addr;

	*_p = data;
}

void BigEndian_StoreWord(void *addr, unsigned short data)
{
	unsigned char *_p = addr;

	*_p = (unsigned char)(data >> 8);
	_p++;
	*_p = (unsigned char)(data >> 0);

	/*
	unsigned short *_p = addr;

	*_p = data;
	*/
}

void BigEndian_StoreDWord(void *addr, unsigned long data)
{
	unsigned char *_p = addr;

	*_p = (unsigned char)(data >> 24);
	_p++;
	*_p = (unsigned char)(data >> 16);
	_p++;
	*_p = (unsigned char)(data >> 8);
	_p++;
	*_p = (unsigned char)(data >> 0);

	/*
	unsigned long *_p = addr;

	*_p = data;
	*_p */
}

unsigned char BigEndian_LoadByte(void *addr)
{
	unsigned char *_p = addr;

	return (*_p);
}

unsigned short BigEndian_LoadWord(void *addr)
{
	unsigned char *_p = addr;
	unsigned short _val, _buf;

	_val = 0;
	_buf = *_p;
	_val |= ((_buf & 0x00FF) << 8);
	_p++;
	_buf = *_p;
	_val |= ((_buf & 0x00FF) << 0);

	return _val;

	/*
	unsigned short *_p = addr;

	return (*_p);
	*/
}

unsigned long BigEndian_LoadDWord(void *addr)
{
	unsigned char *_p = addr;
	unsigned long _val, _buf;

	_val = 0;
	_buf = *_p;
	_val |= ((_buf & 0x000000FF) << 24);
	_p++;
	_buf = *_p;
	_val |= ((_buf & 0x000000FF) << 16);
	_p++;
	_buf = *_p;
	_val |= ((_buf & 0x000000FF) << 8);
	_p++;
	_buf = *_p;
	_val |= ((_buf & 0x000000FF) << 0);

	return _val;

	/*

	unsigned long *_p = addr;

	return (*_p);
	*/
}
